package com.irdstudio.apicenter.console.dm;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import org.springframework.stereotype.Component;

import com.irdstudio.apicenter.frame.core.jdbc.util.ConnectionManager;

@Component(value = "appCommodityShowDao")
public class AppCommodityShowDao {

	/**
	 * 根据主键查询单条记录
	 * 
	 * @param
	 * @return
	 * @throws SQLException
	 */
	public AppCommodityShow queryWithKeys(String key, String appid) throws SQLException {
		AppCommodityShow appCommodityShow = null;
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn
					.prepareStatement("SELECT * FROM app_commodity_show WHERE record_key=? and app_id=?");
			ps.setString(1, key);
			ps.setString(2, appid);
			rs = ps.executeQuery();
			if (rs.next()) {
				appCommodityShow = new AppCommodityShow();
				appCommodityShow.setRecord_key(rs.getString("record_key"));
				appCommodityShow.setApp_id(rs.getString("app_id"));
				appCommodityShow.setCommodity_name(rs
						.getString("commodity_name"));
				appCommodityShow.setCommodity_file_path(rs
						.getString("commodity_file_path"));
				appCommodityShow
						.setCommodity_src(rs.getString("commodity_src"));
				appCommodityShow.setJump_url(rs.getString("jump_url"));
				appCommodityShow.setLong_desc(rs.getString("long_desc"));
			}
		} catch (SQLException e) {
			throw new SQLException(
					"query app_commodity_show WithKeys is Wrong!"
							+ e.getMessage());
		} finally {
			close2(rs, conn, ps);
		}
		return appCommodityShow;
	}

	/**
	 * 插入单条记录
	 * 
	 * @param
	 * @return
	 * @throws SQLException
	 */
	public int save(AppCommodityShow appCommodityShow) throws SQLException {
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			String sql = "insert into app_commodity_show(record_key, app_id, commodity_name, commodity_file_path, commodity_src, jump_url, long_desc) "
					+ " values(?,?,?,?,?,?,?)";
			ps = conn.prepareStatement(sql);
			ps.setString(1, appCommodityShow.getRecord_key());
			ps.setString(2, appCommodityShow.getApp_id());
			ps.setString(3, appCommodityShow.getCommodity_name());
			ps.setString(4, appCommodityShow.getCommodity_file_path());
			ps.setString(5, appCommodityShow.getCommodity_src());
			ps.setString(6, appCommodityShow.getJump_url());
			ps.setString(7, appCommodityShow.getLong_desc());
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("insert app_commodity_show is Wrong!"
					+ e.getMessage());
		} finally {
			close1(conn, ps);
		}
		return flag;
	}

	/**
	 * 修改记录
	 * 
	 * @param
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */

	public int update(AppCommodityShow appCommodityShow) throws SQLException,
			ClassNotFoundException {
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			String sql = "update app_commodity_show set commodity_name=? ,commodity_file_path=? ,commodity_src=? ,jump_url=? ,long_desc=? where record_key=? and app_id=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, appCommodityShow.getCommodity_name());
			ps.setString(2, appCommodityShow.getCommodity_file_path());
			ps.setString(3, appCommodityShow.getCommodity_src());
			ps.setString(4, appCommodityShow.getJump_url());
			ps.setString(5, appCommodityShow.getLong_desc());
			ps.setString(6, appCommodityShow.getRecord_key());
			ps.setString(7, appCommodityShow.getApp_id());
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException(
					"update app_commodity_show WithKeys is Wrong!"
							+ e.getMessage());
		} finally {
			close1(conn, ps);
		}
		return flag;
	}

	/**
	 * 删除模块参数
	 * 
	 * @param appModuleParam
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public int delete(String appid, String recordKey) throws SQLException,
			ClassNotFoundException {
		Connection conn = ConnectionManager.getConnection();
		PreparedStatement ps = null;
		int flag;
		try {
			String sql = "delete from app_commodity_show where record_key=? and app_id=?";
			ps = conn.prepareStatement(sql);
			ps.setString(1, recordKey);
			ps.setString(2, appid);
			flag = ps.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException(
					"delete app_commodity_show WithKeys is Wrong!"
							+ e.getMessage());
		} finally {
			close1(conn, ps);
		}

		return flag;
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close(ResultSet theRs, Statement theStmt,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (theStmt != null)
				theStmt.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close1(Connection conn, PreparedStatement thePs) {
		try {
			if (conn != null)
				conn.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 关闭资源
	 * 
	 * @param theRs
	 * @param theStmt
	 * @param thePs
	 */
	protected void close2(ResultSet theRs, Connection conn,
			PreparedStatement thePs) {
		try {
			if (theRs != null)
				theRs.close();
			if (conn != null)
				conn.close();
			if (thePs != null)
				thePs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
